<?php

// **************************************************************
// | Name: 星空框架 用户管理
// **************************************************************
// | Version: V5.0
// **************************************************************
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// **************************************************************
// | City: 青岛
// **************************************************************
// | Time: 2021-10-28 19:09
// **************************************************************
declare(strict_types=1);

namespace app\xingkong\controller\v1;

use app\common\common\BackendController;
use app\common\model\User as UserModel;
use app\common\model\UserAchievement as UserAchievementModel;
use app\common\model\UserLabel as UserLabelModel;
use app\common\model\UserLevel as UserLevelModel;
use app\common\model\BackMoney as BackMoneyModel;
use app\common\model\UserUpLog as UserUpLogModel;
use app\common\model\SubOrder as SubOrderModel;
use app\common\controller\Huigongbao;
use app\common\services\UserServices;
use think\facade\Db;
use think\facade\View;

/**
 * @xk 用户管理
 */
class User extends BackendController
{
    // ***************
    // | 用户标签管理
    // ***************
    /**
     * @xk 用户标签列表
     *
     * @method userLabel
     * @param  UserLabelModel $userLabelModel            [description]
     * @return [type]         [description]
     * @Time   2021-10-28
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userLabel(UserLabelModel $userLabelModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $condition = [];
            if ($keywords != '') {
                $condition[] = ['user_label_name','like','%'.$keywords.'%'];
            }
            $resultData = [];
            $resultData['list'] = $userLabelModel
              ->where($condition)
              ->field('`user_label_id`,`user_label_name`,`status`,`update_time`')
              ->page($page, $limit)
              ->order('create_time desc')
              ->select()
              ->toArray();
            $resultData['count'] = $userLabelModel->where($condition)->count();
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        return View::fetch();
    }
    /**
     * @xk 用户标签添加
     *
     * @method userLabelCreate
     * @return [type]          [description]
     * @Time   2021-10-28
     * @City   青岛
     * @Author xkyinzhe        <zhonghuaxinxing@sina.cn>
     */
    public function userLabelCreate()
    {
        return View::fetch();
    }
    /**
     * @xk 用户标签编辑
     *
     * @method userLabelEdit
     * @param  UserLabelModel $userLabelModel            [description]
     * @return [type]         [description]
     * @Time   2021-10-28
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userLabelEdit(UserLabelModel $userLabelModel)
    {
        $param = request()->param();
        $user_label_id = isset($param['user_label_id']) ? $param['user_label_id'] : 0;
        $info = $userLabelModel->find($user_label_id);
        return View::fetch('', ['info'=>$info]);
    }
    /**
     * @xk 用户标签保存
     *
     * @method labelSave
     * @param  UserLabelModel $userLabelModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function labelSave(UserLabelModel $userLabelModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $userLabelModel->validate($param, $act);
            switch ($act) {
          case 'create':
            $res = $userLabelModel->create($param);
            break;
          case 'edit':
            $res = $userLabelModel->find($param['user_label_id'])->save($param);
            break;
        }
            Db::commit();
            $result_code = 200;
            $result_msg = '更新成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 用户标签动态保存
     *
     * @method userLabelChanageValue
     * @param  UserLabelModel        $userLabelModel            [description]
     * @return [type]                [description]
     * @Time   2021-10-28
     * @City   青岛
     * @Author xkyinzhe              <zhonghuaxinxing@sina.cn>
     */
    public function userLabelChanageValue(UserLabelModel $userLabelModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $by_key = isset($param['by_key']) ? $param['by_key'] : '';
            $i = 0;
            $j = 0;
            if (!$by_key) {
                throw new \think\Exception("请选择需要修改的数据", 404);
            }
            $by_key_arr = explode(',', $by_key);
            foreach ($by_key_arr as $key => $value) {
                $newarr = [];
                $newarr['by_key'] = $value;
                $newarr['key'] = $param['key'];
                $newarr['value'] = $param['value'];
                $res = $userLabelModel->chanageValue($newarr);
                if ($res) {
                    ++$i;
                }
            }
            $result_code = 200;
            $result_msg = '处理成功';
            Db::commit();
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
            Db::rollback();
        }

        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 用户标签删除
     *
     * @method userLabelDestroy
     * @param  UserLabelModel   $userLabelModel            [description]
     * @return [type]           [description]
     * @Time   2021-10-28
     * @City   青岛
     * @Author xkyinzhe         <zhonghuaxinxing@sina.cn>
     */
    public function userLabelDestroy(UserLabelModel $userLabelModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $id_str = isset($param['id_str']) ? $param['id_str'] : '';
            $i = 0;
            $j = 0;
            if (!$id_str) {
                throw new \think\Exception("请选择需要删除的数据", 404);
            }
            $id_arr = explode(',', $id_str);
            foreach ($id_arr as $key => $value) {
                $res = $userLabelModel->destroy($value);
                ++$i;
            }
            Db::commit();
            $result_code = 200;
            $result_msg = '处理成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    // ***************
    // | 代理等级管理
    // ***************
    /**
     * @xk 代理等级管理
     *
     * @method userLevel
     * @param  UserLevelModel $userLevelModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userLevel(UserLevelModel $userLevelModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $condition = [];
            if ($keywords != '') {
                $condition[] = ['level_name','like','%'.$keywords.'%'];
            }
            $resultData = [];
            $resultData['list'] = $userLevelModel
                                  ->where($condition)
                                  ->field('`user_level_id`,`level_name`,`level`,`buy_num`,`only_achievement_price`,`team_achievement_price`,`child_num`,`need_level`,`agency_bili`,`only_agency_bili`,`agent_bili`,`only_agent_bili`,`create_time`,`picurl`,`avg_bili`')
                                  ->withAttr('picurl', function ($value, $data) {
                                      return getShowUrl($value);
                                  })
                                  ->page($page, $limit)
                                  ->order('level asc')
                                  ->select()
                                  ->toArray();
            $resultData['count'] = $userLevelModel->where($condition)->count();
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        return View::fetch();
    }
    /**
     * @xk 用户等级编辑
     * @method                                      userLevelEdit
     * @param                                       UserLevelModel $userLevelModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-23Tfalse13:false48:false43+080
     */
    public function userLevelEdit(UserLevelModel $userLevelModel)
    {
        $param = request()->param();
        $user_level_id = isset($param['user_level_id']) ? $param['user_level_id'] : 0;
        $info = $userLevelModel->field('*,picurl as show_picurl')
        ->withAttr('show_picurl', function ($value, $data) {
            return getShowUrl($value);
        })
        ->find($user_level_id);
        return View::fetch('', ['info'=>$info]);
    }
    /**
     * @xk 会员等级编辑
     * @method                                      userLevelSave
     * @param                                       UserLevelModel $userLevelModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-23Tfalse13:false49:false55+080
     */
    public function userLevelSave(UserLevelModel $userLevelModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $userLevelModel->validate($param, $act);
            switch ($act) {
          case 'create':
            $res = $userLevelModel->create($param);
            break;
          case 'edit':
            $res = $userLevelModel->find($param['user_level_id'])->save($param);
            break;
        }
            Db::commit();
            $result_code = 200;
            $result_msg = '更新成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 会员等级动态修改
     *
     * @method userLevelChanageValue
     * @param  UserLevelModel        $userLevelModel            [description]
     * @return [type]                [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe              <zhonghuaxinxing@sina.cn>
     */
    public function userLevelChanageValue(UserLevelModel $userLevelModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $by_key = isset($param['by_key']) ? $param['by_key'] : '';
            $i = 0;
            $j = 0;
            if (!$by_key) {
                throw new \think\Exception("请选择需要修改的数据", 404);
            }
            $by_key_arr = explode(',', $by_key);
            foreach ($by_key_arr as $key => $value) {
                $newarr = [];
                $newarr['by_key'] = $value;
                $newarr['key'] = $param['key'];
                $newarr['value'] = $param['value'];
                $res = $userLevelModel->chanageValue($newarr);
                if ($res) {
                    ++$i;
                }
            }
            $result_code = 200;
            $result_msg = '处理成功';
            Db::commit();
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
            Db::rollback();
        }

        $this->result($code, $msg, $result_code, $result_msg);
    }
    // ****************
    // | 会员列表
    // *****************
    /**
     * @xk 会员列表
     *
     * @method userList
     * @param  UserModel      $userModel                 [description]
     * @param  UserLabelModel $userLabelModel            [description]
     * @param  UserLevelModel $userLevelModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userList(UserModel $userModel, UserLabelModel $userLabelModel, UserLevelModel $userLevelModel, UserAchievementModel $userAchievementModel)
    {
        $param = request()->param();
        $now_level = isset($param['now_level']) ? $param['now_level'] : 0; // 当前会员层级
        $l_user_sn = isset($param['l_user_sn']) ? $param['l_user_sn'] : '';
        $parent_user_sn = isset($param['parent_user_sn']) ? $param['parent_user_sn'] : '';
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $level = isset($param['level']) ? $param['level'] : '';
            $status = isset($param['status'])?$param['status']:'';
            $user_label_id = isset($param['user_label_id']) ? $param['user_label_id'] : '';
            $create_time = isset($param['create_time']) ? $param['create_time'] : ''; // 注册时间
            $bind_time = isset($param['bind_time']) ? $param['bind_time'] : '';
            $agent_time = isset($param['agent_time']) ? $param['agent_time'] : '';
            $min_purchases_order_price = isset($param['min_purchases_order_price']) ? $param['min_purchases_order_price'] : 0.00;
            $max_purchases_order_price = isset($param['max_purchases_order_price']) ? $param['max_purchases_order_price'] : 0.00;
            $min_back_money = isset($param['min_back_money']) ? $param['min_back_money'] : 0.00;
            $max_back_money = isset($param['max_back_money']) ? $param['max_back_money'] : 0.00;
            $has_l_user = isset($param['has_l_user']) ? $param['has_l_user'] : '';
            $search_user_sn = isset($param['search_user_sn']) ? $param['search_user_sn'] : '';
            $condition = [];
            if ($keywords != '') {
                $condition[] = ['u.user_name|u.name|u.tel|u.user_sn','like','%'.$keywords.'%'];
            }
            if ($level != '') {
                $condition[] = ['u.level','in',$level];
            }
            if ($user_label_id != '') {
                $condition[] = ['u.user_label_id','in',$user_label_id];
            }
            if ($l_user_sn > 0) {
                $condition[] = ['u.l_user_sn','=',$l_user_sn]; // 上级元素
            }
            if ($min_purchases_order_price > 0) {
                $condition[] = ['u.purchases_order_price','>=',$min_purchases_order_price];
            }
            if ($max_purchases_order_price > 0) {
                $condition[] = ['u.purchases_order_price','<=',$max_purchases_order_price];
            }
            if ($min_back_money > 0) {
                $condition[] = ['u.all_back_money','>=',$min_back_money];
            }
            if ($max_back_money > 0) {
                $condition[] = ['u.all_back_money','<=',$max_back_money];
            }
            if($status != ''){
              $condition[] = ['u.status','in',$status];
            }
            switch ($has_l_user) {
              case '1':
                $condition[] = ['u.l_user_sn','>','0'];
                break;
              case '2':
                $condition[] = ['u.l_user_sn','=',''];
                break;
            }
            if ($search_user_sn != '') {
                $condition[] = ['u.l_user_sn','=',$search_user_sn];
            }
            if ($create_time != '') {
                $search_time_arr = explode('~', $create_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['u.create_time', 'between time', $start_time.','.$end_time];
            }
            if ($bind_time != '') {
                $search_time_arr = explode('~', $bind_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['u.bind_time', 'between time', $start_time.','.$end_time];
            }
            if ($agent_time != '') {
                $search_time_arr = explode('~', $agent_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['u.agent_time', 'between time', $start_time.','.$end_time];
            }
            $resultData = [];
            $resultData['count'] = $userModel
                             ->alias('u')
                             ->where($condition)
                             ->count();
            $list = $userModel
                            ->alias('u')
                            ->leftJoin('user lu', 'u.l_user_sn=lu.user_sn')
                            ->leftJoin('userLevel ul', 'ul.level=u.level')
                            ->leftJoin('userLabel ub', 'ub.user_label_id=u.user_label_id')
                            ->where($condition)
                            ->field('u.`user_id`,u.`user_sn`,u.`level`,u.`user_name`,u.`user_label_id`,u.`head_pic`,ub.`user_label_name`,u.`tel`,u.`l_user_sn`,ul.`level_name`,u.`all_back_money`,u.`back_money`,u.`create_time`,u.`bind_time`,u.`agent_time`,lu.`user_name` as l_user_name,u.`all_achievement_price`,u.`agent_achievement_price`,u.`status`,u.`status` as status_txt,u.`is_check`,u.`card_num`,u.`level_1_count`,u.`level_2_count`,u.`level_3_count`,u.`level_4_count`,u.`level_5_count`,u.`purchases_num`,u.`purchases_order_price`,u.`purchases_achievement_price`')
                            ->withAttr('head_pic', function ($value, $data) {
                                return getShowUrl($value);
                            })
                            ->withAttr('status_txt', function ($value, $data) {
                                return $value==1 ? '<span style="color:green;">正常</span>' : '<span style="color:red;">异常</span>';
                            })
                            ->withAttr('l_user_sn', function ($value, $data) {
                                return $value>0 ? $value : '暂无';
                            })
                            ->page($page, $limit)
                            ->group('u.user_sn')
                            ->order('create_time desc')
                            ->select()
                            ->toArray();
            $resultData['list'] = $list;

            $all_count = $userModel->alias('u')->where($condition)->count(); // 会员总数
            $all_back_money = $userModel->alias('u')->where($condition)->sum('back_money');
            $all_all_back_money = $userModel->alias('u')->where($condition)->sum('all_back_money');

            $resultData['show_count'] = '会员总数：'.$all_count.'人，余额:'.$all_back_money.'元，总佣金'.$all_all_back_money.'元';
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        $user_label_list = $userLabelModel->field('`user_label_id`,`user_label_name`')->select()->toArray();
        $user_level_list = $userLevelModel->field('`level`,`level_name`')->select()->toArray();
        return View::fetch('', ['now_level'=>(int)$now_level,'l_user_sn'=>$l_user_sn,'parent_user_sn'=>$parent_user_sn,'user_level_list'=>$user_level_list,'user_label_list'=>$user_label_list]);
    }
    /**
     * @xk 导出筛选后会员
     * @method          exportUserList
     * @param           UserModel $userModel  [description]
     * @param           UserLabelModel $userLabelModel  [description]
     * @param           UserLevelModel $userLevelModel  [description]
     * @param           UserAchievementModel $userAchievementModel  [description]
     * @return          [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe <zhonghuaxinxing@sina.cn>
     * @City：           青岛
     * @Time：2022-04-19
     */
    public function exportUserList(UserModel $userModel, UserLabelModel $userLabelModel, UserLevelModel $userLevelModel, UserAchievementModel $userAchievementModel)
    {
        $param = request()->param();
        $now_level = isset($param['now_level']) ? $param['now_level'] : 0; // 当前会员层级
        $l_user_sn = isset($param['l_user_sn']) ? $param['l_user_sn'] : '';
        $parent_user_sn = isset($param['parent_user_sn']) ? $param['parent_user_sn'] : '';
        $keywords = isset($param['keywords']) ? $param['keywords'] : '';
        $level = isset($param['level']) ? $param['level'] : '';
        $status = isset($param['status']) ? $param['status'] : '';
        $user_label_id = isset($param['user_label_id']) ? $param['user_label_id'] : '';
        $create_time = isset($param['create_time']) ? $param['create_time'] : ''; // 注册时间
        $bind_time = isset($param['bind_time']) ? $param['bind_time'] : '';
        $agent_time = isset($param['agent_time']) ? $param['agent_time'] : '';
        $min_purchases_order_price = isset($param['min_purchases_order_price']) ? $param['min_purchases_order_price'] : 0.00;
        $max_purchases_order_price = isset($param['max_purchases_order_price']) ? $param['max_purchases_order_price'] : 0.00;
        $min_back_money = isset($param['min_back_money']) ? $param['min_back_money'] : 0.00;
        $max_back_money = isset($param['max_back_money']) ? $param['max_back_money'] : 0.00;
        $has_l_user = isset($param['has_l_user']) ? $param['has_l_user'] : '';
        $search_user_sn = isset($param['search_user_sn']) ? $param['search_user_sn'] : '';
        $condition = [];
        if ($keywords != '') {
            $condition[] = ['u.user_name|u.name|u.tel|u.user_sn','like','%'.$keywords.'%'];
        }
        if ($level != '') {
            $condition[] = ['u.level','in',$level];
        }
        if ($user_label_id != '') {
            $condition[] = ['u.user_label_id','in',$user_label_id];
        }
        if ($l_user_sn > 0) {
            $condition[] = ['u.l_user_sn','=',$l_user_sn]; // 上级元素
        }
        if ($min_purchases_order_price > 0) {
            $condition[] = ['u.purchases_order_price','>=',$min_purchases_order_price];
        }
        if ($max_purchases_order_price > 0) {
            $condition[] = ['u.purchases_order_price','<=',$max_purchases_order_price];
        }
        if ($min_back_money > 0) {
            $condition[] = ['u.all_back_money','>=',$min_back_money];
        }
        if ($max_back_money > 0) {
            $condition[] = ['u.all_back_money','<=',$max_back_money];
        }
        if($status != ''){
          $condition[] = ['u.status','in',$status];
        }
        switch ($has_l_user) {
          case '1':
            $condition[] = ['u.l_user_sn','>','0'];
            break;
          case '2':
            $condition[] = ['u.l_user_sn','=',''];
            break;
        }
        if ($search_user_sn != '') {
            $condition[] = ['u.l_user_sn','=',$search_user_sn];
        }
        if ($create_time != '') {
            $search_time_arr = explode('~', $create_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            $condition[] = ['u.create_time', 'between time', $start_time.','.$end_time];
        }
        if ($bind_time != '') {
            $search_time_arr = explode('~', $bind_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            $condition[] = ['u.bind_time', 'between time', $start_time.','.$end_time];
        }
        if ($agent_time != '') {
            $search_time_arr = explode('~', $agent_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            $condition[] = ['u.agent_time', 'between time', $start_time.','.$end_time];
        }
        $allCount = $userModel->alias('u')->where($condition)->count();
        $limit = 5000;
        $pages = ceil($allCount / $limit);
        for ($i = 1; $i <= $pages; ++$i) {
            $user_list = $userModel
                        ->alias('u')
                        ->leftJoin('user lu', 'u.l_user_sn=lu.user_sn')
                        ->leftJoin('userLevel ul', 'ul.level=u.level')
                        ->leftJoin('userLabel ub', 'ub.user_label_id=u.user_label_id')
                        ->where($condition)
                        ->field('u.`user_id`,u.`user_sn`,u.`level`,u.`user_name`,u.`user_label_id`,ub.`user_label_name`,u.`tel`,u.`l_user_sn`,ul.`level_name`,u.`all_back_money`,u.`back_money`,u.`create_time`,u.`bind_time`,u.`agent_time`,lu.`user_name` as l_user_name,u.`all_achievement_price`,u.`agent_achievement_price`,u.`status` as status_txt,u.`card_num`,u.`purchases_order_price`')
                        ->withAttr('status_txt', function ($value, $data) {
                            return $value==1 ? '正常' : '异常';
                        })
                        ->withAttr('l_user_sn', function ($value, $data) {
                            return $value>0 ? $value : '暂无';
                        })
                        ->page($i, $limit)
                        ->order('create_time desc')
                        ->select()
                        ->toArray();
            $order_data = [];
            if ($user_list) {
                $new_list = [];
                foreach ($user_list as $key => $value) {
                    $arrays = [];
                    $arrays[] = (float)$value['user_sn'];
                    $arrays[] = replace_specialChar($value['user_name']);
                    $arrays[] = $value['tel'];
                    // $arrays[] = $value['card_num'];
                    $arrays[] = (float)$value['l_user_sn'];
                    $arrays[] = replace_specialChar($value['l_user_name']);
                    $arrays[] = $value['level_name'];
                    $arrays[] = (float)$value['all_back_money'];
                    $arrays[] = (float)$value['back_money'];
                    $arrays[] = (float)$value['all_achievement_price'];
                    $arrays[] = (float)$value['agent_achievement_price'];
                    $arrays[] = (float)$value['purchases_order_price'];
                    $arrays[] = $value['user_label_name'];
                    $arrays[] = $value['status_txt'];
                    $arrays[] = $value['create_time'];
                    $arrays[] = $value['bind_time'];
                    $arrays[] = $value['agent_time'];
                    $order_data[] = $arrays;
                }
                $title = ['会员编号','会员昵称','手机号','上级会员编号','上级会员昵称','当前等级','总佣金','余额','团队总业绩','个人业绩','消费总金额','用户标签','会员状态','注册时间','绑定时间','代理时间'];
                $width = [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20];
                $path = '/export/userList';
                $result = $userModel->export($title, $order_data, $path, $width, $i,'会员表');
                $url_arr[] = $result;
            }
        }
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 查看下级会员
     *
     * @method userNextList
     * @param  UserLabelModel $userLabelModel            [description]
     * @param  UserLevelModel $userLevelModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-11
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userNextList(UserModel $userModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        if (request()->isAjax()) {
            $child_list = $userModel->field('`user_sn`,`l_user_sn`')->select()->toArray();
            $childs = getTeamChilds($child_list, $user_sn);
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 100;
            $condition = [];
            $condition[] = ['u.user_sn','in',$childs];
            $user_list = $userModel
               ->alias('u')
               ->leftJoin('userLevel ul', 'u.level = ul.level')
               ->where($condition)
               ->field('u.`user_sn`,u.`user_name`,u.`tel`,u.`l_user_sn`,u.`all_achievement_price`,u.`agent_achievement_price`,u.`all_back_money`,u.`purchases_order_price`,ul.`level_name`,u.`create_time`,u.`agent_time`,u.`bind_time`')
               ->page($page, $limit)
               ->select();
            if($user_list){
              $user_list = $user_list->toArray();
              foreach ($user_list as $key => $value) {
                if($value['l_user_sn'] == $user_sn){
                  $user_list[$key]['develop'] = 'v1';
                }else{
                  $user_list[$key]['develop'] = 'vn';
                }
              }
            }
            $resultData = [];
            $resultData['list'] = $user_list;
            $resultData['count'] = $userModel->alias('u')->where($condition)->count();
            $this->result(200, '加载成功', 200, '请求成功', $resultData);
        }
        return View::fetch('', ['user_sn'=>$user_sn]);
    }
    /**
     * @xk 导出下级会员表
     * @method          exportUserNextList
     * @param           UserModel $userModel  [description]
     * @return          [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe <zhonghuaxinxing@sina.cn>
     * @City：           青岛
     * @Time：2022-04-19
     */
    public function exportUserNextList(UserModel $userModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        $child_list = $userModel->field('`user_sn`,`l_user_sn`')->select()->toArray();
        $childs = getTeamChilds($child_list, $user_sn);
        $limit = 10000;
        $condition = [];
        $condition[] = ['u.user_sn','in',$childs];
        $allCount = $userModel->alias('u')->where($condition)->count();
        $pages = ceil($allCount / $limit);
        for ($i = 1; $i <= $pages; ++$i) {
            $user_list = $userModel
                      ->alias('u')
                      ->leftJoin('userLevel ul', 'u.level = ul.level')
                      ->leftJoin('user lu','lu.user_sn = u.l_user_sn')
                      ->where($condition)
                      ->field('u.`user_sn`,u.`user_name`,u.`tel`,u.`l_user_sn`,u.`all_achievement_price`,u.`agent_achievement_price`,u.`all_back_money`,u.`purchases_order_price`,ul.`level_name`,u.`create_time`,lu.`user_name` as l_user_name,u.`bind_time`,u.`agent_time`')
                      ->page($i, $limit)
                      ->select()
                      ->toArray();
            $order_data = [];
            if ($user_list) {
                $new_list = [];
                foreach ($user_list as $key => $value) {
                    $arrays = [];
                    if($value['l_user_sn'] == $user_sn){
                      $arrays[] = 'v1';
                    }else{
                      $arrays[] = 'vn';
                    }
                    $arrays[] = (string)$value['user_sn'];
                    $arrays[] = replace_specialChar($value['user_name']);
                    $arrays[] = $value['tel'];
                    $arrays[] = $value['l_user_sn'];
                    $arrays[] = $value['l_user_name'];
                    $arrays[] = $value['level_name'];
                    $arrays[] = $value['all_back_money'];
                    $arrays[] = $value['all_achievement_price'];
                    $arrays[] = $value['agent_achievement_price'];
                    $arrays[] = $value['purchases_order_price'];
                    $arrays[] = $value['create_time'];
                    $arrays[] = $value['bind_time'];
                    $arrays[] = $value['agent_time'];
                    $order_data[] = $arrays;
                }
                $title = ['代数','会员编号','会员昵称','手机号','上级会员编号','上级会员昵称','当前等级','总佣金','团队总业绩','个人业绩','消费总金额','注册时间','绑定时间','代理时间'];
                $width = [10,20,20,20,20,20,20,20,20,20,20,20,20,20];
                $path = '/export/userNextList';
                $result = $userModel->export($title, $order_data, $path, $width, $i,'下级会员表');
                $url_arr[] = $result;
            }
        }
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 追溯上级
     * @method          parentList
     * @param           UserModel $userModel  [description]
     * @return          [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe <zhonghuaxinxing@sina.cn>
     * @City：           青岛
     * @Time：2022-04-18
     */
    public function parentList(UserModel $userModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        if (request()->isAjax()) {
            $user_list = [];
            $user_list = $userModel->getParentList($user_sn);
            $resultData['list'] = $user_list;
            $resultData['count'] = count($user_list);
            $this->result(200, '加载成功', 200, '请求成功', $resultData);
        }
        return View::fetch('', ['user_sn'=>$user_sn]);
    }
    /**
     * @xk 导出上级列表
     * @method          exportparentList
     * @param           UserModel $userModel  [description]
     * @return          [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe <zhonghuaxinxing@sina.cn>
     * @City：           青岛
     * @Time：2022-04-19
     */
    public function exportParentList(UserModel $userModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn'])?$param['user_sn']:'';
        $user_list = $userModel->getParentList($user_sn);
        $order_data = [];
        if ($user_list) {
            $new_list = [];
            foreach ($user_list as $key => $value) {
                $arrays = [];
                $arrays[] = (string)$value['user_sn'];
                $arrays[] = replace_specialChar($value['user_name']);
                $arrays[] = $value['tel'];
                $arrays[] = $value['l_user_sn'];
                $arrays[] = $value['level_name'];
                $arrays[] = $value['all_back_money'];
                $arrays[] = $value['all_achievement_price'];
                $arrays[] = $value['agent_achievement_price'];
                $arrays[] = $value['purchases_order_price'];
                $arrays[] = $value['create_time'];
                $order_data[] = $arrays;
            }
            $title = ['会员编号','会员昵称','手机号','上级会员编号','当前等级','总佣金','团队总业绩','个人业绩','消费总金额','注册时间'];
            $width = [20,20,20,20,20,20,20,20,20,20];
            $path = '/export/userParentList';
            $result = $userModel->export($title, $order_data, $path, $width, 1,'上级会员表');
            $url_arr[] = $result;
        }

        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 换绑上级
     *
     * @method chanageLUserSn
     * @param  UserServices   $userServices              [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function chanageLUserSn(UserServices $userServices)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $l_user_sn = isset($param['l_user_sn']) ? $param['l_user_sn'] : '';
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $userServices->syncChanageUser($user_sn, $l_user_sn);

            $result_code = 200;
            $result_msg = '换绑上级成功';
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 发放其他收益
     *
     * @method sendMoney
     * @param  UserServices $userServices              [description]
     * @return [type]       [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe     <zhonghuaxinxing@sina.cn>
     */
    public function sendMoney(UserServices $userServices)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $money = isset($param['money']) ? $param['money'] : 0.00;
            $content = isset($param['content']) ? $param['content'] : '';
            $userServices->syncSendBackMoney($user_sn, $money, $content);
            Db::commit();
            $result_code = 200;
            $result_msg = '发放其他收益成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 更换用户标签
     *
     * @method chanageLabel
     * @param  UserModel    $userModel                 [description]
     * @return [type]       [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe     <zhonghuaxinxing@sina.cn>
     */
    public function chanageLabel(UserModel $userModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $user_label_id = isset($param['user_label_id']) ? $param['user_label_id'] : 0;
            if ($user_label_id <= 0) {
                throw new \think\Exception("请选择用户标签", 404);
            }
            $user_res = $userModel->where([['user_sn','=',$user_sn]])->find()->save(['user_label_id'=>$user_label_id]);
            Db::commit();
            $result_code = 200;
            $result_msg = '更换标签成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 更换用户手机号
     * @method                                      chanageTel
     * @param                                       UserModel $userModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-25Tfalse17:false40:false22+080
     */
    public function chanageTel(UserModel $userModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $tel = isset($param['tel']) ? $param['tel'] : '';
            if ($tel ==  '') {
                throw new \think\Exception("请输入需要更换的用户手机号", 404);
            }
            $count = $userModel->where([['tel','=',$tel],['user_sn','<>',$user_sn]])->count();
            if ($count > 0) {
                throw new \think\Exception("手机号已经绑定其他账号，请重试", 404);
            }
            $user_res = $userModel->where([['user_sn','=',$user_sn]])->find()->save(['tel'=>$tel]);
            Db::commit();
            $result_code = 200;
            $result_msg = '换绑手机号成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 修改密码
     * @method                                      chanagePass
     * @param                                       UserModel $userModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-25Tfalse17:false46:false14+080
     */
    public function chanagePass(UserModel $userModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $pass = isset($param['pass']) ? $param['pass'] : '';
            $pass1 = isset($param['pass1']) ? $param['pass1'] : '';
            if ($pass ==  '') {
                throw new \think\Exception("请输入用户密码", 404);
            }
            if ($pass ==  '') {
                throw new \think\Exception("请输入确认密码", 404);
            }
            $param['old_pass'] = '123456';
            $userModel->validate($param, 'editPass'); // 注册信息验证  验证手机号 密码 验证码等
            // 开始修改密码
            $res = $userModel
                  ->where([['user_sn', '=', $param['user_sn']]])
                  ->find()
                  ->save(['pass' => $pass,'next_pass'=>$pass1,'is_pass_bind'=>1]);
            Db::commit();
            $result_code = 200;
            $result_msg = '修改密码成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 修改会员状态
     * @method                                      chanageStatus
     * @param                                       UserModel $userModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-25Tfalse17:false47:false18+080
     */
    public function chanageStatus(UserModel $userModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $status = isset($param['status']) ? $param['status'] : '';
            $res = $userModel
                  ->where([['user_sn', '=', $param['user_sn']]])
                  ->find()
                  ->save(['status' => $status]);
            Db::commit();
            $result_code = 200;
            $result_msg = '会员状态修改成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 更换会员等级
     * @method  chanageLevel
     * @param   UserModel $userModel  [description]
     * @return  [type] [description]
     * @Version V5.0
     * @Time    2021-11-10
     * @City    青岛
     * @Author  xkyinzhe <zhonghuaxinxing@sina.cn>
     */
    public function chanageLevel(UserModel $userModel, UserUpLogModel $userUpLogModel, UserServices $userServices)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
            $level = isset($param['user_level']) ? $param['user_level'] : 0;
            $old_level = isset($param['old_level']) ? $param['old_level'] : '';
            if ($level <= 0) {
                throw new \think\Exception("请选择需要更换的会员等级", 404);
            }
            $user_data = [];
            $user_data['level'] = $level;
            $user_data['is_buy'] = 1;
            // if ($old_level < 3 && $level >= 3) {
                // $user_data['agent_time'] = date('Y-m-d H:i:s');
            // }
            if($level >= 3){
              // 2022-05-28 此处应销售需求，更改成为代理时间为 等级大于县代的情况下动态更新代理时间
              $user_data['agent_time'] = date('Y-m-d H:i:s');
            }
            $user_info = $userModel->where([['user_sn','=',$user_sn]])->field('`user_id`,`user_sn`,`l_user_sn`')->find();
            $user_info = $user_info ? $user_info->toArray() : [];
            $user_res = $userModel->where([['user_sn','=',$user_sn]])->find()->save($user_data);
            $user_up_data = [];
            $user_up_data['user_sn'] = $user_sn; // 会员
            $user_up_data['old_level'] = $old_level; // 升级之前会员等级
            $user_up_data['level'] = $level; // 升级后会员等级
            $user_up_data['content'] = '后台调整会员等级'; // 升级后会员等级
            $userUpLogModel->create($user_up_data); // 写入用户升级记录

            // 调整会员等级后 同步会员相关
            if($user_info['l_user_sn'] > 0){
              $userServices->syncCheckUp($user_info['user_sn'], $user_info['l_user_sn'],$user_info['user_sn'], $user_info['l_user_sn'], $old_level, 'desc'); // 就会员同步旧数
              $userServices->syncCheckUp($user_info['user_sn'], $user_info['l_user_sn'], $user_info['user_sn'], $user_info['l_user_sn'], $level, 'inc'); // 新会员同步新数据
            }


            Db::commit();
            $result_code = 200;
            $result_msg = '更换会员等级成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    /**
     * @xk 获取银行卡信息
     * @method                                 getBankInfo
     * @param                                  Huigongbao $huigongbao  [description]
     * @return                                 [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                        <zhonghuaxinxing@sina.cn>
     * @City：                                  青岛
     * @Time：2022-02-26T09:false11:false40+080
     */
    public function getBankInfo(Huigongbao $huigongbao)
    {
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        $param = request()->param();
        $card_num = isset($param['card_num']) ? $param['card_num'] : '';
        try {
            if ($card_num == '') {
                throw new \think\Exception("请检查是否完善过信息", 404);
            }
            $info = $huigongbao->getBankInfo(trim($card_num));
            // dump($info); die();
            $resultData = $info;
            $result_code = 200;
            $result_msg = '加载成功';
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    // *****************
    // | 会员业绩
    // ******************
    /**
     * @xk 会员业绩列表
     *
     * @method achievement
     * @param  UserAchievementModel $userAchievementModel      [description]
     * @return [type]               [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe             <zhonghuaxinxing@sina.cn>
     */
     public function achievement(UserModel $userModel, UserLevelModel $userLevelModel, UserAchievementModel $userAchievementModel)
     {
         $param = request()->param();
         if (request()->isAjax()) {
             $page = isset($param['page'])?(int)$param['page']:1;
             $limit = isset($param['limit'])?(int)$param['limit']:30;
             $keywords = isset($param['keywords'])?$param['keywords']:'';
             $level = isset($param['level'])?$param['level']:'';
             $search_time = isset($param['search_time'])?$param['search_time']:'';
             $max_achievement_price = isset($param['max_achievement_price'])?$param['max_achievement_price']:0.00;
             $min_achievement_price = isset($param['min_achievement_price'])?$param['min_achievement_price']:0.00;
             $max_team_achievement_price = isset($param['max_team_achievement_price'])?$param['max_team_achievement_price']:0.00;
             $min_team_achievement_price = isset($param['min_team_achievement_price'])?$param['min_team_achievement_price']:0.00;
             $max_month_achievement_price = isset($param['max_month_achievement_price'])?$param['max_month_achievement_price']:0.00;
             $min_month_achievement_price = isset($param['min_month_achievement_price'])?$param['min_month_achievement_price']:0.00;
             $condition = [];
             if ($keywords != '') {
                 $condition[] = ['u.user_sn|u.user_name','like','%'.$keywords.'%'];
             }
             if ($level != '') {
                 $condition[] = ['u.level','in',$level];
             }
             $condition[] = ['u.all_achievement_price','>',0];
            if ($max_achievement_price) {
               $condition[] = ['u.agent_achievement_price','<=',$max_achievement_price];
           }
           if ($min_achievement_price) {
               $condition[] = ['u.agent_achievement_price','>=',$min_achievement_price];
           }
           if ($max_team_achievement_price) {
               $condition[] = ['u.all_achievement_price','<=',$max_team_achievement_price];
           }
           if ($min_team_achievement_price) {
               $condition[] = ['u.all_achievement_price','>=',$min_team_achievement_price];
           }
           // sleep(20); // 缓步加载
             // 获取存在业绩的会员
             $user_list = $userModel
                          ->alias('u')
                          ->leftJoin('userLevel ul', 'ul.level = u.level')
                          ->where($condition)
                          ->field('u.`user_name`,u.`user_sn`,ul.`level_name`,u.`agent_achievement_price`')
                          ->page($page, $limit)
                          ->order('u.create_time desc')
                          ->select()
                          ->toArray();
             if ($user_list) {
                 foreach ($user_list as $key => $value) {
                     // 开始根据条件进行业绩统计
                     $achievement_condition = [];
                     $achievement_condition[] = ['user_sn','=',$value['user_sn']];
                     // 开始获取当月业绩
                     $now_month = date('Y-m');
                     $month_achievement_price = $userAchievementModel->where($achievement_condition)->where([['create_time','like',$now_month.'%']])->sum('achievement_price');
                     $month_only_achievement_price = $userAchievementModel->where($achievement_condition)->where([['create_time','like',$now_month.'%'],['create_user_sn','=',$value['user_sn']]])->sum('achievement_price');
                     // 开始获取总业绩
                     if ($search_time != '') {
                         $search_time_arr = explode('~', $search_time);
                         $start_time = $search_time_arr[0];
                         $end_time = $search_time_arr[1];
                         $achievement_condition[] = ['create_time', 'between time', $start_time.','.$end_time];
                     }
                     $all_achievement_price = $userAchievementModel->where($achievement_condition)->sum('achievement_price');
                     // $all_only_achievement_price = $userAchievementModel
                     //                               ->where($achievement_condition)
                     //                               ->where([['create_user_sn','=',$value['user_sn']]])
                     //                               ->sum('achievement_price');
                     // $user_list[$key]['all_only_achievement_price'] = $all_only_achievement_price;
                     $user_list[$key]['all_achievement_price'] = $all_achievement_price;
                     $user_list[$key]['month_achievement_price'] = $month_achievement_price;
                     $user_list[$key]['month_only_achievement_price'] = $month_only_achievement_price;
                     $user_list[$key];
                 }
             }
             $resultData = [];
             $resultData['count'] = $userModel->alias('u')->where($condition)->count();
             // $resultData['count'] = $all_count;
             $resultData['list'] = $user_list;
             $this->result(200, '请求成功', 200, '加载成功', $resultData);
         }
         $user_level_list = $userLevelModel->field('`level`,`level_name`')->select()->toArray();
         return View::fetch('', ['user_level_list'=>$user_level_list]);
     }
    /**
     * @xk 会员业绩详情
     * @method                                      achievementShow
     * @param                                       UserModel $userModel  [description]
     * @param                                       UserAchievementModel $userAchievementModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-26Tfalse11:false11:false30+080
     */
    public function achievementShow(UserModel $userModel, UserAchievementModel $userAchievementModel)
    {
        $param = request()->param();
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $search_time = isset($param['search_time']) ? $param['search_time'] : '';
            $condition = [];
            if ($keywords != '') {
                $condition[] = ['ua.user_sn|ua.sub_order_sn|ua.push_user_sn','like','%'.$keywords.'%'];
            }
            $condition[] = ['ua.user_sn','=',$user_sn];
            if ($search_time) {
                $search_time_arr = explode('~', $search_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                $condition[] = ['ua.create_time', 'between time', $start_time.','.$end_time];
            }
            $user_list = $userAchievementModel
                       ->alias('ua')
                       ->leftJoin('user pu', 'pu.user_sn = ua.create_user_sn')
                       ->where($condition)
                       ->field('ua.`sub_order_sn`,ua.`achievement_price`,ua.`create_user_sn`,ua.`user_sn`,pu.`user_name` as push_user_name,ua.`create_time`')
                       ->page($page, $limit)
                       ->order('ua.create_time desc')
                       ->select()
                       ->toArray();
            // 开始查询 合计数据
            $all_achievement_price = $userAchievementModel->alias('ua')->where($condition)->sum('achievement_price');
            $only_achievement_price = $userAchievementModel->alias('ua')->where($condition)->where([['create_user_sn','=',$user_sn]])->sum('achievement_price');
            $resultData = [];
            $resultData['show_count']  = '查询时间内，个人总业绩：'.$only_achievement_price.'元,团队总业绩'.$all_achievement_price.'元';
            $resultData['count'] = $userAchievementModel->alias('ua')->where($condition)->count();
            $resultData['list'] = $user_list;
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        return View::fetch('', ['user_sn'=>$user_sn]);
    }
    /**
     * @xk 会员业绩导出
     *
     * @method exportAchievement
     * @param  UserModel            $userModel                 [description]
     * @param  UserLevelModel       $userLevelModel            [description]
     * @param  UserAchievementModel $userAchievementModel      [description]
     * @return [type]               [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe             <zhonghuaxinxing@sina.cn>
     */
    public function exportAchievement(UserModel $userModel, UserLevelModel $userLevelModel, UserAchievementModel $userAchievementModel)
    {
        $param = request()->param();
        $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
        $keywords = isset($param['keywords'])?$param['keywords']:'';
        $level = isset($param['level'])?$param['level']:'';
        $search_time = isset($param['search_time'])?$param['search_time']:'';
        $max_achievement_price = isset($param['max_achievement_price'])?$param['max_achievement_price']:0.00;
        $min_achievement_price = isset($param['min_achievement_price'])?$param['min_achievement_price']:0.00;
        $max_team_achievement_price = isset($param['max_team_achievement_price'])?$param['max_team_achievement_price']:0.00;
        $min_team_achievement_price = isset($param['min_team_achievement_price'])?$param['min_team_achievement_price']:0.00;
        $max_month_achievement_price = isset($param['max_month_achievement_price'])?$param['max_month_achievement_price']:0.00;
        $min_month_achievement_price = isset($param['min_month_achievement_price'])?$param['min_month_achievement_price']:0.00;
        $condition = [];
        if ($keywords != '') {
            $condition[] = ['u.user_sn|u.user_name','like','%'.$keywords.'%'];
        }
        if ($level != '') {
            $condition[] = ['u.level','in',$level];
        }
        $condition[] = ['u.all_achievement_price','>',0];
       if ($max_achievement_price) {
          $condition[] = ['u.agent_achievement_price','<=',$max_achievement_price];
      }
      if ($min_achievement_price) {
          $condition[] = ['u.agent_achievement_price','>=',$min_achievement_price];
      }
      if ($max_team_achievement_price) {
          $condition[] = ['u.all_achievement_price','<=',$max_team_achievement_price];
      }
      if ($min_team_achievement_price) {
          $condition[] = ['u.all_achievement_price','>=',$min_team_achievement_price];
      }
      // sleep(30); // 缓步加载
        $allCount = $userModel->alias('u')->where($condition)->count();
        $limit = 10000;
        $pages = ceil($allCount / $limit);
        for ($i = 1; $i <= $pages; ++$i) {
            $user_list = $userModel
                    ->alias('u')
                    ->leftJoin('userLevel ul', 'ul.level = u.level')
                    ->where($condition)
                    ->field('u.`user_name`,u.`user_sn`,ul.`level_name`,u.`agent_achievement_price`')
                    ->page($i, $limit)
                    ->order('u.create_time desc')
                    ->select()
                    ->toArray();
            $order_data = [];
            if ($user_list) {
                $new_list = [];
                foreach ($user_list as $key => $value) {
                  // 开始根据条件进行业绩统计
                  $achievement_condition = [];
                  $achievement_condition[] = ['user_sn','=',$value['user_sn']];
                  // 开始获取当月业绩
                  $now_month = date('Y-m');
                  $month_achievement_price = $userAchievementModel->where($achievement_condition)->where([['create_time','like',$now_month.'%']])->sum('achievement_price');
                  $month_only_achievement_price = $userAchievementModel->where($achievement_condition)->where([['create_time','like',$now_month.'%'],['create_user_sn','=',$value['user_sn']]])->sum('achievement_price');
                  // 开始获取总业绩
                  if ($search_time != '') {
                      $search_time_arr = explode('~', $search_time);
                      $start_time = $search_time_arr[0];
                      $end_time = $search_time_arr[1];
                      $achievement_condition[] = ['create_time', 'between time', $start_time.','.$end_time];
                  }
                  $all_achievement_price = $userAchievementModel->where($achievement_condition)->sum('achievement_price');
                  // $all_only_achievement_price = $userAchievementModel
                  //                               ->where($achievement_condition)
                  //                               ->where([['create_user_sn','=',$value['user_sn']]])
                  //                               ->sum('achievement_price');
                    $arrays = [];
                    $arrays[] = $value['user_name'];
                    $arrays[] = $value['user_sn'];
                    $arrays[] = $value['level_name'];
                    $arrays[] = $all_achievement_price;
                    $arrays[] = $value['agent_achievement_price'];
                    $arrays[] = $month_achievement_price;
                    $arrays[] = $month_only_achievement_price;
                    $order_data[] = $arrays;
                }
                $title = ['会员昵称','会员编号','会员等级','团队累计总业绩','个人总业绩','当月团队总业绩','当月个人总业绩'];
                $width = [20,20,20,15,15,15];
                $path = '/export/userAchievement';
                $result = $userAchievementModel->export($title, $order_data, $path, $width, $i,'会员业绩表');
                $url_arr[] = $result;
            }
        }
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    // ******************
    // | 升级记录
    // *********************
    /**
     * @xk 升级记录
     *
     * @method userUpLog
     * @param  UserModel      $userModel                 [description]
     * @param  UserUpLogModel $userUpLogModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function userUpLog(UserModel $userModel, UserLevelModel $userLevelModel, UserUpLogModel $userUpLogModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $level = isset($param['level']) ? $param['level'] : '';
            $old_level = isset($param['old_level']) ? $param['old_level'] : '';
            $search_time = isset($param['search_time']) ? $param['search_time'] : '';
            $condition = [];
            if ($keywords != '') {
                $user_condition = [];
                $user_condition[] = ['user_name|tel|user_sn','like','%'.$keywords.'%'];
                $user_sn_arr = $userModel->where($user_condition)->column('user_sn');
                $condition[] = ['up.user_sn','in',$user_sn_arr];
            }
            if ($level != '') {
                $condition[] = ['up.level','in',$level];
            }
            if ($old_level != '') {
                $condition[] = ['up.old_level','in',$old_level];
            }
            if ($search_time) {
                $search_time_arr = explode('~', $search_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['up.create_time', 'between time', $start_time.','.$end_time];
            }
            $resultData = [];
            $resultData['count'] = $userUpLogModel->alias('up')->where($condition)->count();
            $resultData['list'] = $userUpLogModel
                            ->alias('up')
                            ->leftJoin('user u', 'up.user_sn = u.user_sn')
                            ->leftJoin('userLevel ul', 'up.level = ul.level')
                            ->leftJoin('userLevel ulo', 'up.old_level = ulo.level')
                            ->where($condition)
                            ->field('up.`user_sn`,up.`level`,u.`tel`,u.`user_name`,ul.`level_name`,ulo.`level_name` as old_level_name,up.`content`,up.`create_time`')
                            ->order('create_time desc')
                            ->page($page, $limit)
                            ->select()
                            ->toArray();
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        $user_level_list = $userLevelModel->field('`level`,`level_name`')->select()->toArray();
        return View::fetch('', ['user_level_list'=>$user_level_list]);
    }
    /**
     * @xk 导出升级记录
     *
     * @method exportUserUpLog
     * @param  UserModel       $userModel                 [description]
     * @param  UserUpLogModel  $userUpLogModel            [description]
     * @return [type]          [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe        <zhonghuaxinxing@sina.cn>
     */
    public function exportUserUpLog(UserModel $userModel, UserUpLogModel $userUpLogModel)
    {
        $param = request()->param();
        $limit = 10000;
        $keywords = isset($param['keywords']) ? $param['keywords'] : '';
        $level = isset($param['level']) ? $param['level'] : '';
        $search_time = isset($param['search_time']) ? $param['search_time'] : '';
        $condition = [];
        if ($keywords != '') {
            $user_condition = [];
            $user_condition[] = ['user_name|tel|user_sn','like','%'.$keywords.'%'];
            $user_sn_arr = $userModel->where($user_condition)->column('user_sn');
            $condition[] = ['up.user_sn','in',$user_sn_arr];
        }
        if ($level != '') {
            $condition[] = ['up.level','in',$level];
        }
        if ($search_time) {
            $search_time_arr = explode('~', $search_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
            // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
            $condition[] = ['up.create_time', 'between time', $start_time.','.$end_time];
        }

        $allCount = $userUpLogModel->alias('up')->where($condition)->count();
        $limit = 10000;
        $pages = ceil($allCount / $limit);
        for ($i = 1; $i <= $pages; ++$i) {
            $data = $userUpLogModel
                  ->alias('up')
                  ->leftJoin('user u', 'up.user_sn = u.user_sn')
                  ->leftJoin('userLevel ul', 'up.level = ul.level')
                  ->leftJoin('userLevel ulo', 'up.old_level = ulo.level')
                  ->where($condition)
                  ->field('up.`user_sn`,up.`level`,u.`tel`,u.`user_name`,u.`tel`,ul.`level_name`,ulo.`level_name` as old_level_name,up.`create_time`,up.`content`')
                  ->select()
                  ->toArray();
            $order_data = [];
            if ($data) {
                foreach ($data as $key => $value) {
                    $arrays = [];
                    $arrays[] = $value['user_name'];
                    $arrays[] = $value['user_sn'];
                    $arrays[] = $value['tel'];
                    $arrays[] = $value['old_level_name'];
                    $arrays[] = $value['level_name'];
                    $arrays[] = $value['content'];
                    $arrays[] = $value['create_time'];
                    $order_data[] = $arrays;
                }
                $title = ['会员昵称','会员编号','手机号','原等级','晋升等级','备注','晋升时间'];
                $width = [20,20,20,15,15,60,25];
                $path = '/export/userUpLog';
                $result = $userUpLogModel->export($title, $order_data, $path, $width, $i,'升级记录表');
                $url_arr[] = $result;
            }
        }
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    // *******************
    // | 代理订单
    // ********************
    /**
     * @xk 代理订单列表
     *
     * @method agentOrder
     * @param  UserModel      $userModel                 [description]
     * @param  SubOrderModel  $subOrderModel             [description]
     * @param  UserLevelModel $userLevelModel            [description]
     * @param  BackMoneyModel $backMoneyModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function agentOrder(UserModel $userModel, SubOrderModel $subOrderModel, UserLevelModel $userLevelModel, BackMoneyModel $backMoneyModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $is_avg_finance = isset($param['is_avg_finance']) ? $param['is_avg_finance'] : ''; // 是否审核
            $is_check = isset($param['is_check']) ? $param['is_check'] : ''; // 是否出账
            $avg_finance_time = isset($param['avg_finance_time']) ? $param['avg_finance_time'] : ''; // 结算时间
            $check_time = isset($param['check_time']) ? $param['check_time'] : ''; // 出账时间
            $create_time = isset($param['create_time']) ? $param['create_time'] : ''; // 下单时间
            $condition = [];
            if ($keywords != '') {
                $user_condition = [];
                $user_condition[] = ['user_name|tel|user_sn','like','%'.$keywords.'%'];
                $user_sn_arr = $userModel->where($user_condition)->column('user_sn');
                if ($user_sn_arr) {
                    $condition[] = ['sub.user_sn','in',$user_sn_arr];
                } else {
                    $condition[] = ['sub.sub_order_sn','like','%'.$keywords.'%'];
                }
            }
            if ($is_avg_finance != '') {
                $condition = [];
                $condition[] = ['sub.is_avg_finance','in',$is_avg_finance];
            }
            if ($is_check != '') {
                $condition[] = ['sub.is_check','in',$is_check];
            }
            $condition[] = ['sub.status','=','3'];
            if ($create_time) {
                $search_time_arr = explode('~', $create_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['sub.create_time', 'between time', $start_time.','.$end_time];
            }
            if ($avg_finance_time) {
                $search_time_arr = explode('~', $avg_finance_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['sub.avg_finance_time', 'between time', $start_time.','.$end_time];
            }
            if ($check_time) {
                $search_time_arr = explode('~', $check_time);
                $start_time = $search_time_arr[0];
                $end_time = $search_time_arr[1];
                // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
                // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
                $condition[] = ['sub.check_time', 'between time', $start_time.','.$end_time];
            }
            $resultData = [];
            $resultData['count'] = $subOrderModel->alias('sub')->where($condition)->count();
            $list = $subOrderModel
                    ->alias('sub')
                    ->leftJoin('user u', 'u.user_sn = sub.user_sn')
                    ->leftJoin('userAchievement ua', 'ua.sub_order_sn = sub.sub_order_sn and ua.user_sn = sub.user_sn')
                    ->where($condition)
                    ->field('sub.`sub_order_sn`,sum(ua.`achievement_price`) as all_achievement_price,sub.`is_finance`,sub.`is_check`,sub.`finance_time`,sub.`check_time`,u.`user_name`,u.`user_sn`,u.`level`')
                    ->withAttr('is_finance', function ($value, $data) {
                        return $value==1 ? '已结算' : '未结算';
                    })
                    ->withAttr('is_check', function ($value, $data) {
                        return $value==1 ? '已出账' : '未出账';
                    })
                    ->page($page, $limit)
                    ->order('sub.receiving_time desc')
                    ->group('sub.sub_order_id')
                    ->select()
                    ->toArray();
            if ($list) {
                foreach ($list as $key => $value) {
                    // 获取会员等级
                    $list[$key]['level_name'] = $userLevelModel->where([['level','=',$value['level']]])->value('level_name');
                    // 综合佣金金额
                    $list[$key]['all_back_money'] = $backMoneyModel
                                                    ->where([['act','in','2,3'],['sub_order_sn','=',$value['sub_order_sn']]])
                                                    ->sum('money');
                }
            }
            $resultData['list'] = $list;
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        return View::fetch();
    }
    /**
     * @xk 代理订单导出
     *
     * @method exportAgentOrder
     * @param  UserModel        $userModel                 [description]
     * @param  SubOrderModel    $subOrderModel             [description]
     * @param  UserLevelModel   $userLevelModel            [description]
     * @param  BackMoneyModel   $backMoneyModel            [description]
     * @return [type]           [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe         <zhonghuaxinxing@sina.cn>
     */
    public function exportAgentOrder(UserModel $userModel, SubOrderModel $subOrderModel, UserLevelModel $userLevelModel, BackMoneyModel $backMoneyModel)
    {
        $param = request()->param();
        $limit = 10000;
        $keywords = isset($param['keywords']) ? $param['keywords'] : '';
        $is_avg_finance = isset($param['is_avg_finance']) ? $param['is_avg_finance'] : ''; // 是否审核
      $is_check = isset($param['is_check']) ? $param['is_check'] : ''; // 是否出账
      $avg_finance_time = isset($param['avg_finance_time']) ? $param['avg_finance_time'] : ''; // 结算时间
      $check_time = isset($param['check_time']) ? $param['check_time'] : ''; // 出账时间
      $create_time = isset($param['create_time']) ? $param['create_time'] : ''; // 下单时间
      $condition = [];
        if ($keywords != '') {
            $user_condition = [];
            $user_condition[] = ['user_name|tel|user_sn','like','%'.$keywords.'%'];
            $user_sn_arr = $userModel->where($user_condition)->column('user_sn');
            $condition[] = ['sub.user_sn','in',$user_sn_arr];
        }
        if ($is_avg_finance != '') {
            $condition = [];
            $condition[] = ['sub.is_avg_finance','in',$is_avg_finance];
        }
        if ($is_check != '') {
            $condition[] = ['sub.is_check','in',$is_check];
        }
        $condition[] = ['sub.status','=','3'];
        if ($create_time) {
            $search_time_arr = explode('~', $create_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
            // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
            $condition[] = ['sub.create_time', 'between time', $start_time.','.$end_time];
        }
        if ($avg_finance_time) {
            $search_time_arr = explode('~', $avg_finance_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
            // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
            $condition[] = ['sub.avg_finance_time', 'between time', $start_time.','.$end_time];
        }
        if ($check_time) {
            $search_time_arr = explode('~', $check_time);
            $start_time = $search_time_arr[0];
            $end_time = $search_time_arr[1];
            // $start_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[0]).' 00:00:00')); //当前时间0点开始
            // $end_time = date('Y-m-d H:i:s', strtotime(trim($search_time_arr[1]).' 23:59:59')); //当前时间0点开始
            $condition[] = ['sub.check_time', 'between time', $start_time.','.$end_time];
        }

        $allCount = $subOrderModel->alias('sub')->where($condition)->count();
        $limit = 10000;
        $pages = ceil($allCount / $limit);
        for ($i = 1; $i <= $pages; ++$i) {
            $data = $subOrderModel
                  ->alias('sub')
                  ->leftJoin('user u', 'u.user_sn = sub.user_sn')
                  ->where($condition)
                  ->field('sub.`sub_order_sn`,sub.`all_achievement_price`,sub.`is_avg_finance`,sub.`is_check`,sub.`avg_finance_time`,sub.`check_time`,u.`user_name`,u.`user_sn`,u.`level`')
                  ->withAttr('is_avg_finance', function ($value, $data) {
                      return $value==1 ? '已结算' : '未结算';
                  })
                  ->withAttr('is_check', function ($value, $data) {
                      return $value==1 ? '已出账' : '未出账';
                  })
                  ->page($i, $limit)
                  ->order('sub.create_time desc')
                  ->select()
                  ->toArray();
            $order_data = [];
            if ($data) {
                foreach ($data as $key => $value) {
                    $arrays = [];
                    $arrays[] = $value['user_name'];
                    $arrays[] = $value['user_sn'];
                    $arrays[] = $userLevelModel->where([['level','=',$value['level']]])->value('level_name');
                    $arrays[] = $value['sub_order_sn'];
                    $arrays[] = $value['all_achievement_price'];
                    $arrays[] = $backMoneyModel->where([['act','in','1,2,3'],['sub_order_sn','=',$value['sub_order_sn']]])->sum('money');
                    $arrays[] = $value['is_avg_finance'];
                    $arrays[] = $value['is_check'];
                    $arrays[] = $value['avg_finance_time'];
                    $arrays[] = $value['check_time'];
                    $order_data[] = $arrays;
                }
                $title = ['会员昵称','会员编号','会员等级','订单编号','业绩金额','佣金总额','是否结算','是否出账','结算时间','出账时间'];
                $width = [20,20,20,20,15,15,15,15,20,20];
                $path = '/export/agentOrder';
                $result = $subOrderModel->export($title, $order_data, $path, $width, $i,'代理订单表');
                $url_arr[] = $result;
            }
        }
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '导出成功';
        $resultData = $url_arr;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 代理订单详情
     *
     * @method agentOrderShow
     * @param  BackMoneyModel $backMoneyModel            [description]
     * @return [type]         [description]
     * @Time   2021-11-09
     * @City   青岛
     * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
     */
    public function agentOrderShow(BackMoneyModel $backMoneyModel)
    {
        $param = request()->param();
        $sub_order_sn = isset($param['sub_order_sn']) ? $param['sub_order_sn'] : '';
        if (request()->isAjax()) {
            $condition = [];
            $condition[] = ['bm.sub_order_sn','=',$sub_order_sn];
            $condition[] = ['bm.act','in','2,3,5'];
            $resultData = [];
            $resultData['list'] = $backMoneyModel
                              ->alias('bm')
                              ->leftJoin('user u', 'u.user_sn = bm.user_sn')
                              ->leftJoin('userLevel ul', 'ul.level = bm.user_level')
                              ->where($condition)
                              ->field('u.`user_name`,u.`user_sn`,bm.`act`,bm.`sub_order_sn`,bm.`money`,bm.`achievement_price`,ul.`level_name`,`is_finance`,`finance_time`')
                              ->withAttr('act', function ($value, $data) {
                                  return getBonusType($value);
                              })
                              ->withAttr('is_finance', function ($value, $data) {
                                  return $value==0 ? '未出账' : ($value==1 ? '<span style="color:green;">出账成功</span>' : '<span style="color:red;">出账失败</span>');
                              })
                              ->order('bm.back_money_id desc')
                              ->select()
                              ->toArray();
            $resultData['count'] = $backMoneyModel->alias('bm')->where($condition)->count();
            $this->result(200, '请求成功', 200, '加载成功', $resultData);
        }
        return View::fetch('', ['sub_order_sn'=>$sub_order_sn]);
    }
}
